package Server;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Server extends JFrame implements ActionListener {

    private JButton close;
    public JTextArea user;
    private ServerSocket server;
    public Socket fileClient;
    public Hashtable<String, ClientConnect> listUser;
    public File file = new File("C:/error/tmp");

    public Server() {
        super("Chat: Server");
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                try {
                    //gởi tin nhắn tới tất cả client
                    server.close();
                    System.exit(0);
                } catch (IOException e1) {
                }
            }
        });
        setSize(400, 400);
        addItem();
        setVisible(true);
    }

    private void addItem() {
        setLayout(new BorderLayout());

        add(new JLabel("Trang thai server: \n"), BorderLayout.NORTH);
        add(new JPanel(), BorderLayout.EAST);
        add(new JPanel(), BorderLayout.WEST);

        user = new JTextArea(10, 20);
        user.setEditable(false);
        add(new JScrollPane(user), BorderLayout.CENTER);

        close = new JButton("Close Server");
        close.addActionListener(this);
        add(close, BorderLayout.SOUTH);
    }

    private void go() {
        try {
            listUser = new Hashtable<String, ClientConnect>();
            server = new ServerSocket(1391);
            user.append("May chu duoc mo\n");
            while (true) {
                Socket client = server.accept();
                new ClientConnect(this, client);

            }
        } catch (IOException e) {
            user.append("Khong khoi dong duoc may chu\n");
        } catch (Exception e) {
        }
    }

    public static void main(String[] args) {
        new Server().go();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            server.close();
        } catch (IOException e1) {
            user.append("Khong dung duoc may chu\n");
        }
        System.exit(0);
    }

    public void sendAll(String from, String msg) {
        Enumeration e = listUser.keys();
        String name = null;
        while (e.hasMoreElements()) {
            name = (String) e.nextElement();
            //System.out.println(name);
            if (name.compareTo(from) != 0) {
                listUser.get(name).sendMSG("3", msg);
            }
        }
    }

    public void sendAllUpdate(String from) {
        Enumeration e = listUser.keys();
        String name = null;
        while (e.hasMoreElements()) {
            name = (String) e.nextElement();
            //System.out.println(name);
            if (name.compareTo(from) != 0) {
                listUser.get(name).sendMSG("4", getAllName());
            }
        }
    }

    public void forwardFile(final String from, final String nameFile) {
        try {
            final Enumeration e = listUser.keys();
            String name = null;
            while (e.hasMoreElements()) {
                name = (String) e.nextElement();
                if (name.compareTo(from) != 0) {
                    listUser.get(name).sendMSG("5", nameFile);
                }
            }
        } catch (Exception ex) {
        }
    }

    public String getAllName() {
        Enumeration e = listUser.keys();
        String name = "";
        while (e.hasMoreElements()) {
            name += (String) e.nextElement() + "\n";
        }
        return name;
    }

}
